<template>
  <div>
    <div class="addFormBox">
      <!-- 循环data中定义的数组 -->
      <div v-for="(item, index) in formLabelAlign" :key="index">
        <div class="formOuterBox">
          <div class="formCotantBox">
            <h3>车辆信息 {{ index + 1 }}</h3>
            <!-- 表单内容 -->
            <el-form label-width="80px">
              <el-form-item label="车牌号">
                <el-input v-model="item.carBoard"></el-input>
              </el-form-item>
              <el-form-item label="车牌颜色">
                <el-input v-model="item.carColor"></el-input>
              </el-form-item>
              <el-form-item label="排放阶段">
                <el-input v-model="item.discharge"></el-input>
              </el-form-item>
            </el-form>
            <el-button @click="test"></el-button>
          </div>
          <!-- 操作按钮 -->
          <div>
            <el-button v-if="formLabelAlign.length > 1" @click="removeIdx(item, index)" type="danger">删除此条车辆信息</el-button>
          </div>
        </div>
      </div>
      <el-button @click="addForm" type="success">添加车辆信息</el-button>
    </div>
    <div>
      <el-table :row-class-name="tableRowClassName" :data="formLabelAlignTwo" border style="width: 100%">
        <el-table-column align="center" width="100px" type="index" label="序号"></el-table-column>
        <el-table-column align="center" prop="name" label="编号">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="rylx" label="燃油类型">
          <template slot-scope="scope">
            <el-select @change="rylxChange(scope.row.rylx, scope.row.index)" size="mini" v-model="scope.row.rylx"
              placeholder="请选择燃油类型">
              <el-option v-for="item in rylxOptions" :key="item.value" :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="yh" label="油号">
          <template slot-scope="scope">
            <el-select size="mini" v-model="scope.row.yh">
              <el-option v-for="item in scope.row.yhOptions" :key="item.label" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="address" label="地址">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.address"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="date" label="操作">
          <template slot-scope="scope">
            <i @click="addFormTwo(scope.row, scope.$index)" class="el-icon-circle-plus"
              v-if="formLabelAlignTwo.length == scope.$index + 1"></i>
            <i v-if="formLabelAlignTwo.length > 1" @click="removeIdxTwo(scope.row, scope.$index)" style="color:rgb(216,30,6)"
              class="el-icon-remove"></i>
          </template>
        </el-table-column>

      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单绑定数据
      formLabelAlign: [
        {
          carBoard: "",
          carColor: "",
          discharge: "",
        },
      ],
      formLabelAlignTwo: [
        {
          name: "",
          rylx: "",
          yh: "",
          address: "",
          yhOptions: [
            {
              value: "1",
              label: "5",
            },
            {
              value: "2",
              label: "0",
            },
            {
              value: "3",
              label: "-10",
            },
            {
              value: "4",
              label: "-20",
            },
            {
              value: "5",
              label: "-35",
            },
            {
              value: "6",
              label: "-50",
            },
          ],
        },
      ],
      rylxOptions: [
        {
          value: "0",
          label: "汽油",
        },
        {
          value: "1",
          label: "柴油",
        },
      ],
    };
  },
  methods: {
    test() {
      this.$router.push("/demo")
    },
    //   添加操作
    addForm() {
      // 定义一个标识，通过标识判断是否能添加信息
      let statusType = true;
      this.formLabelAlign.forEach((item) => {
        if (
          item.carBoard == "" ||
          item.carColor == "" ||
          item.discharge == ""
        ) {
          this.$message({
            message: "请完善信息后在添加",
            type: "warning",
          });
          statusType = false;
        }
      });
      if (statusType) {
        this.formLabelAlign.push({
          carBoard: "",
          carColor: "",
          discharge: "",
        });
      }
    },
    // 删除操作
    removeIdx(item, index) {
      this.formLabelAlign.splice(index, 1);
      this.$message({
        message: "删除成功",
        type: "success",
      });
    },
    // 添加index
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    // 切换汽油柴油不同数据
    rylxChange(e, index) {
      // 柴油
      if (e == "1") {
        this.formLabelAlignTwo[index].yhOptions = [
          {
            value: "1",
            label: "5",
          },
          {
            value: "2",
            label: "0",
          },
          {
            value: "3",
            label: "-10",
          },
          {
            value: "4",
            label: "-20",
          },
          {
            value: "5",
            label: "-35",
          },
          {
            value: "6",
            label: "-50",
          },
        ];
      } else {
        // 汽油
        this.formLabelAlignTwo[index].yhOptions = [
          {
            value: "1",
            label: "89",
          },
          {
            value: "2",
            label: "92",
          },
          {
            value: "3",
            label: "95",
          },
          {
            value: "4",
            label: "98",
          },
        ];
      }
    },
    // 添加操作
    addFormTwo() {
      let statusType = true;
      this.formLabelAlignTwo.forEach((item) => {
        if (
          item.name == "" ||
          item.rylx == "" ||
          item.yh == "" ||
          item.address == ""
        ) {
          this.$message({
            message: "请完善信息后在添加",
            type: "warning",
          });
          statusType = false;
        }
      });
      if (statusType) {
        this.formLabelAlignTwo.push({
          name: "",
          rylx: "",
          yh: "",
          address: "",
        });
      }
    },
    // 删除操作
    removeIdxTwo(item, index) {
      console.log(item, index);
      this.formLabelAlignTwo.splice(index, 1);
      this.$message({
        message: "删除成功",
        type: "success",
      });
    },
  },
};
</script>

<style scoped>
.addFormBox {
  margin: 20px;
}

.formOuterBox {
  margin-bottom: 20px;
  padding: 30px 40px;
  background: white;
  border-radius: 30px;
}

h3 {
  margin: 0px 0px 20px 0px;
}

i {
  font-size: 24px;
  cursor: pointer;
}
</style>
